package com.financegames.cashflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

public class ManualActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manual_menu);
        findViewById(R.id.about_button).
	        setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alert = new AlertDialog.Builder(ManualActivity.this)
					.setTitle("About the Game")
	                .setMessage(readRawTextFile(ManualActivity.this, R.raw.about))
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        /* User clicked OK so do some stuff */
	                    	dialog.dismiss();
	                    }
	                })
	                .create();
					alert.show();
				}
			});
        
        findViewById(R.id.idea_button).
	        setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alert = new AlertDialog.Builder(ManualActivity.this)
	                .setTitle("Game idea")
					.setMessage(readRawTextFile(ManualActivity.this, R.raw.idea))
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        /* User clicked OK so do some stuff */
	                    	dialog.dismiss();
	                    }
	                })
	                .create();
					alert.show();
				}
			});
        findViewById(R.id.rules_button).
	        setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alert = new AlertDialog.Builder(ManualActivity.this)
	                .setTitle("Rules")
					.setMessage(readRawTextFile(ManualActivity.this, R.raw.rules))
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        /* User clicked OK so do some stuff */
	                    	dialog.dismiss();
	                    }
	                })
	                .create();
					alert.show();
				}
			});
        
        findViewById(R.id.process_button).
	        setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alert = new AlertDialog.Builder(ManualActivity.this)
	                .setTitle("Game Process")
					.setMessage(readRawTextFile(ManualActivity.this, R.raw.game_process))
	                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        /* User clicked OK so do some stuff */
	                    	dialog.dismiss();
	                    }
	                })
	                .create();
					alert.show();
				}
			});
    }
    public static String readRawTextFile(Context ctx, int resId)
    {
         InputStream inputStream = ctx.getResources().openRawResource(resId);

            InputStreamReader inputreader = new InputStreamReader(inputStream);
            BufferedReader buffreader = new BufferedReader(inputreader);
             String line;
             StringBuilder text = new StringBuilder();

             try {
               while (( line = buffreader.readLine()) != null) {
                   text.append(line);
                   text.append('\n');
                 }
           } catch (IOException e) {
               return null;
           }
             return text.toString();
    }
}